package com.itheima.dao.sqlprovider;

import com.itheima.entity.QueryPageBean;

import java.util.Map;

public class CheckGroupProvider {

    /**
     * SQL排序方法一
     * @param map
     * @return
     */
    public String selectByOrder(Map map) {

        Integer[] checkitemIds = (Integer[]) map.get("checkitemIds");
        //SELECT *,if(id=12,1,if(id=10,2,if(id=13,3,4))) as dataorder from t_checkgroup ORDER BY dataorder
        StringBuffer sql;
        //System.out.println(sql);

        if (checkitemIds != null && checkitemIds.length != 0) {
            sql = new StringBuffer("select *,");
            for (int i = 0; i < checkitemIds.length; i++) {
                if (i != checkitemIds.length - 1) {
                    sql.append("if(id=")
                            .append(checkitemIds[i])
                            .append(",")
                            .append(i)
                            .append(",");
                } else {
                    sql.append("if(id=")
                            .append(checkitemIds[i])
                            .append(",")
                            .append(i)
                            .append(",")
                            .append(checkitemIds.length);
                    for (int j = 0; j < checkitemIds.length; j++) {
                        sql.append(")");
                    }
                    sql.append(" as dataorder from t_checkitem order by dataorder");
                }
            }
        } else {
            sql = new StringBuffer("select * from t_checkitem");
        }


        return sql.toString();
    }

    /**
     * sql 排序方法2
     * @param map
     * @return
     */                         //id=5 [1,2]
    public String selectByOrder2(Map map){
        Integer[] checkitemIds = (Integer[]) map.get("checkitemIds");

        StringBuffer sql = new StringBuffer("select * from t_checkitem");
        if (checkitemIds != null && checkitemIds.length != 0) {
            sql.append(" order by case");
            for (int i = 0; i < checkitemIds.length; i++) {
                sql.append(" when id=").append(checkitemIds[i]).append(" then ").append(i);
            }
            sql.append(" else id end asc");
        }

        return sql.toString();
    }
    /*public static void main(String[] args) {

        Integer[] value = new Integer[]{2,5,8,9};

        StringBuffer sql = new StringBuffer("select * from t_checkitem");
        if (value != null && value.length != 0) {
            sql.append(" order by case");
            for (int i = 0; i < value.length; i++) {
                sql.append(" when id=").append(value[i]).append(" then ").append(i);
            }
            sql.append(" else id end asc");
        }
        System.out.println(sql);
    }*/

    /*public static void main(String[] args) {
     *//*StringBuffer sql = new StringBuffer("select *,");
        System.out.println(sql);*//*

        //System.out.println(sql);
        StringBuffer sql;
        Integer[] checkitemIds = new Integer[]{5,9,5,6,88};
        if (checkitemIds!=null && checkitemIds.length !=0) {
            sql = new StringBuffer("select *,");
            for (int i = 0; i < checkitemIds.length; i++) {
                if (i != checkitemIds.length - 1) {
                    sql.append("if(id=").append(checkitemIds[i]).append(",").append(i).append(",");
                } else {
                    sql.append("if(id=").append(checkitemIds[i]).append(",").append(i).append(",").append(checkitemIds.length);
                    for (int j = 0; j < checkitemIds.length; j++) {
                        sql.append(")");
                    }
                    sql.append(" as dataorder from t_checkitem order by dataorder");
                }
            }
        }else{
            sql = new StringBuffer("select * from t_checkitem");
        }
        System.out.println(sql.toString());
    }*/
}